package com.linfnn.Controller;
import com.linfnn.demo.DemoApplication;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;



@SpringBootTest(classes = DemoApplication.class)
@AutoConfigureMockMvc
//@ExtendWith(MockMvcTestExtension.class)
public class Controller {

    @Autowired
    private MockMvc mockMvc;

    @Value("${spring.mail.username}")
    String name;

    //需要发送的目的邮箱地址和内容
    @Autowired
    private JavaMailSender emailSender;

    @Test
    public void sendMail() {

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(name);
        message.setTo("1973950559@qq.com");
        message.setSubject("登录验证码");
        message.setText("尊敬的用户，您的登录验证码为：xxxx。请在登录页面输入此验证码完成登录，验证码仅可使用一次。");
        emailSender.send(message);
    }

    @Test
    public void testLoginSuccess() throws Exception {
        // 模拟成功登录的情况
        String username = "admin1";
        String password = "pass1";

        mockMvc.perform(MockMvcRequestBuilders.post("/submit")
                .param("username", username)
                .param("password", password)
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print());
    }

    @Test
    public void testLoginFailure() throws Exception {
        // 模拟登录失败的情况
        String username = "admin1";
        String password = "pass1xxx";

        mockMvc.perform(MockMvcRequestBuilders.post("/submit")
                .param("username", username)
                .param("password", password)
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print());
    }
}
